package com.wxapp.pay.service.pay;

import com.wxapp.common.core.filter.except.ThrowMyException;
import com.wxapp.common.util.check.CheckUtil;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description
 * @author lgl
 * DateTime  2024/2/5 17:00
 * Modified By
 * 调用链模式 配置支付方式
 */
@Component
@RequiredArgsConstructor
public class PayFactory implements InitializingBean {
    private static final Logger log = LoggerFactory.getLogger(PayFactory.class);
    private final Map<String, BasePay> payWay = new ConcurrentHashMap<>();
    private final List<BasePay> payWays;

    public BasePay choosePayWay(String type) throws ThrowMyException {
        var basePay = payWay.get(type);
        if (CheckUtil.isNullObject(basePay)) {
            throw new ThrowMyException("调用支付方式失败");
        }
        return basePay;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (CheckUtil.isNullList(payWays)) {
            log.error("没有设置支付方式");
            return;
        }
        payWays.forEach(v -> payWay.put(v.getType(), v));
    }
}
